﻿// SettingForm.cpp : Implementation of CSettingForm


#include "stdafx.h"
#include "SettingForm.h"
#include "Parser.h"
#include "Regesiter.h"

#include "../omsproxy/OMSClientConcrete.h"

//#include "OMSClientConcrete.h"

// CSettingForm
LRESULT CSettingForm::BuildUserInfoInternal(const std::wstring& userName , const std::wstring& Password , std::wstring& result)
{
	wchar_t buffer[1000];
	LPCWSTR src = L"<?xml version=\"1.0\" encoding=\"utf-16\"?> \
					<xmsUser client=\"Microsoft Office Outlook 12.0\" xmlns = \"http://schemas.microsoft.com/office/Outlook/2006/OMS\"> \
						<userId>%s</userId> \
						<password>%s</password> \
						<customData/> \
					</xmsUser>";
	wsprintfW(buffer,src,userName.c_str(),Password.c_str());
	result.clear();
	result.append(buffer);
	return 0;
}

bool CSettingForm::CheckURL(const std::wstring& url)
{
	if(url.length() < 9)
		return false;
	if(wcscmp(url.substr(0,8).c_str(),L"https://") == 0)
		return true;
	return false;
}

LRESULT CSettingForm::ParseAndSaveServiceInfo(const std::wstring& serviceInfo, const std::wstring& serviceurl , std::wstring& serviceProvider)
{
	//items we care
	std::wstring serviceUri;
	std::wstring signUpPage;
	std::wstring localName;
	std::wstring SMS_maxRecipientsPerMessage;
	std::wstring SMS_maxMessagesPerSend;
	std::wstring SMS_maxSbcsPerMessage;
	std::wstring SMS_maxDbcsPerMessage;
	Parser* parser = new Parser();
	Regesiter* regesiter = new Regesiter();
	//HKEY hKey = NULL;

	/*parse the xml*/
	if(parser->FindServiceProvider(serviceInfo,serviceProvider)) goto err_realse;
	if(parser->FindServiceUri(serviceInfo,serviceUri)) goto err_realse;
	if(wcscmp(serviceUri.c_str(),serviceurl.c_str()) != 0) goto err_realse; //check if two url is matched
	if(parser->FindSignUpPage(serviceInfo,signUpPage)) goto err_realse;
	if(parser->FindLocalName(serviceInfo,localName)) goto err_realse;
	if(parser->FindSMSmaxRecipientsPerMessage(serviceInfo,SMS_maxRecipientsPerMessage)) goto err_realse;
	if(parser->FindSMSmaxMessagesPerSend(serviceInfo,SMS_maxMessagesPerSend)) goto err_realse;
	if(parser->FindSMSmaxSbcsPerMessage(serviceInfo,SMS_maxSbcsPerMessage)) goto err_realse;
	if(parser->FindSMSmaxDbcsPerMessage(serviceInfo,SMS_maxDbcsPerMessage)) goto err_realse;
	

	//regesiter sevice
	if(regesiter->ServiceInfoRegesiter(
		serviceProvider,
		serviceUri,
		signUpPage,
		localName,
		SMS_maxRecipientsPerMessage,
		SMS_maxMessagesPerSend,
		SMS_maxSbcsPerMessage,
		SMS_maxDbcsPerMessage) != ERROR_SUCCESS) return -1;

	delete parser;
	delete regesiter;
	return 0;
err_realse:
	/*if(hKey)
		RegCloseKey(hKey);*/
	delete parser;
	delete regesiter;
	return -1;
}

LRESULT CSettingForm::ParseAndSaveUserInfo
	(const std::wstring& userInfo,const std::wstring& user, const std::wstring& pwd , const std::wstring& serviceProvider)
{
	//items we care
	std::wstring replyPhone;
	std::wstring smtpAddress;
	std::wstring errorCode;

	Parser* parser = new Parser();
	Regesiter* regesiter = new Regesiter();

	/*parse the xml*/
	if(parser->FindUserInfoErrorCode(userInfo,errorCode)) goto err_release;
	if(wcscmp(errorCode.c_str(),L"ok") != 0) {
		std::wstring errorMsg(L"The service return the error code : ");
		errorMsg.append(errorCode);
		MessageBoxW(errorMsg.c_str(),L"Web service is not responding",MB_ICONWARNING);
		goto err_release;
	}
	parser->FindUserReplyPhone(userInfo,replyPhone);
	parser->FindUserSmtpAddress(userInfo,smtpAddress);
		
	/*save to regedit*/
	if(regesiter->UserInfoRegesiter(
		serviceProvider,
		user,
		pwd,
		replyPhone,
		smtpAddress) != ERROR_SUCCESS) return -1;


	delete parser;
	delete regesiter;
	return 0;

err_release:
	delete parser;
	delete regesiter;
	return -1;
}


LRESULT CSettingForm::OnClickedOK(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	LPCWSTR serviceErrorMsg = L"The web service is not responding , please contact your service provider for more information.";
	LPCWSTR serviceErrorMsgCaption = L"Web service is not responding";
	HWND hEdit = ::GetDlgItem(m_hWnd, IDC_EDIT_URL);
	wchar_t* input = new wchar_t[256];
	::SendMessageW(hEdit,WM_GETTEXT,256,(LPARAM)input);
	std::wstring urlstr(input);
	delete []input;
	//MessageBoxW(urlstr.c_str(),urlstr.c_str(),MB_ICONWARNING);
	std::wstring serviceInfo;
	// TODO check the url firstly
	//if(!CheckURL(urlstr)){
	//	MessageBoxW(L"The url of web service is not correct.please check the url.",L"Url Error",MB_ICONWARNING);
	//	return -1;
	//}
	if(m_client == NULL)
		m_client = (OMSClientBase*)new OMSClientConcrete();
	if(m_client->GetServiceInfo(urlstr,serviceInfo) < 0){
		MessageBoxW(serviceErrorMsg,serviceErrorMsgCaption,MB_ICONWARNING);
		return -1;
	}
	std::wstring serviceProvider;
	if(ParseAndSaveServiceInfo(serviceInfo,urlstr,serviceProvider) < 0) {
		MessageBoxW(serviceErrorMsg,serviceErrorMsgCaption,MB_ICONWARNING);
		return -1;
	}
	//begin to send userinfo to the service
	//get user
	hEdit = ::GetDlgItem(m_hWnd,IDC_EDIT_USER);
	input = new wchar_t[256];
	::SendMessageW(hEdit,WM_GETTEXT,256,(LPARAM)input);
	std::wstring userstr(input);//
	delete []input;
	//get pwd
	hEdit = ::GetDlgItem(m_hWnd,IDC_EDIT_PWD);
	input = new wchar_t[256];
	::SendMessageW(hEdit,WM_GETTEXT,256,(LPARAM)input);
	std::wstring pwdstr(input);//
	delete []input;
	//build
	std::wstring userinfoTobesend;
	std::wstring userinfoResponse;
	if(BuildUserInfoInternal(userstr,pwdstr,userinfoTobesend) < 0){
		MessageBoxW(serviceErrorMsg,serviceErrorMsgCaption,MB_ICONWARNING);
		return -1;
	}
	
	//MessageBoxW(userinfoTobesend.c_str(),L"User info to be sent",MB_ICONWARNING);

	if(m_client->GetUserInfo(urlstr,userinfoTobesend,userinfoResponse) < 0){
		MessageBoxW(serviceErrorMsg,serviceErrorMsgCaption,MB_ICONWARNING);
		return -1;
	}
	if(ParseAndSaveUserInfo(userinfoResponse,userstr,pwdstr,serviceProvider) < 0) {
		MessageBoxW(serviceErrorMsg,serviceErrorMsgCaption,MB_ICONWARNING);
		return -1;
	}

	MessageBoxW(L"Configuration is successful!",L"Success" , MB_OK);
	EndDialog(wID);
	return 0;
}